/**
 * make all apps for single pages
 */
function had_action(method,id) {
	had = getCookie(method+id);
	if (had != null) {
		return true;	
	} else {
		if (has_html5()) {
			had = localStorage.status;
			if (typeof(had)!='undefined'&&had!=null) {
				if (had.indexOf(method+id) != -1) {
					return true;
				}
			}
			
		}		
	}
	
	return false;
}

function set_action(method,id,data) {
	if (data.indexOf('fail') != -1 || data.indexOf('ajax') != -1) return false;
	
	if (has_html5()) {
		localStorage.status += method + id + ',';
		if (localStorage.status.length > 1000000) { // `clear storage if over 1MB
			localStorage.status = localStorage.status.substring(1000);
		}
	}
	setCookie(method+id,'',1000);
	
	return true;
}

// Item common status applications
$('.post-body .preview').click(function(){
	id = Number($(this).attr('data'));
	method = 'pr';
	
	if (had_action(method,id)) return;
	
	$.post(home_url + '/ajax', {
		id:id,
		m:method
	},
	function(data){				
		set_action(method,id,data);
	});
});

$('.post-body .download').click(function(){
	id = Number($(this).attr('data'));
	method = 'dw';
	
	if (had_action(method,id)) return;
	
	$.post(home_url + '/ajax', {
		id:id,
		m:method
	},
	function(data){
		set_action(method,id,data);
	});
});

$('.post-body .install').click(function(){
	id = Number($(this).attr('data'));
	method = 'in';
	
	if (had_action(method,id)) return;
	
	$.post(home_url + '/ajax', {
		id:id,
		m:method
	},
	function(data){
		set_action(method,id,data);
	});
});

// Rate application
$('#side .stat .plus').click(function(){
	id = Number($(this).attr('data'));	
	method = 'ra';
	if (had_action(method,id)) {
		alert('You already rated');
		return;
	}
	
	method = 'ru';
	
	$(this).html('...');
	
	$.post(home_url + '/ajax', {
		id:id,
		m:method
	},
	function(data){		
		method = 'ra';
		if (set_action(method,id,data)) {
			$('#side .stat .rate-up').html(function(id,oldhtml){
				var cur = Number(oldhtml);
				cur++;
				return '+' + cur;
			});
			$('#side .stat .rate').remove();
		} else {
			$(this).html('+');
		}
	});
});

$('#side .stat .minus').click(function(){
	id = Number($(this).attr('data'));
	method = 'ra';
	if (had_action(method,id)) {
		alert('You already rated');
		return;
	}
	
	method = 'rd';
	
	$.post(home_url + '/ajax', {
		id:id,
		m:method
	},
	function(data){
		method = 'ra';
		if (set_action(method,id,data)) {
			$('#side .stat .rate-up').html(function(id,oldhtml){
				var cur = Number(oldhtml);
				cur -= 1;
				return '-' + cur;
			});
			$('#side .stat .rate').remove();
		} else {
			$(this).html('+');
		}	
	});
});


$('#side .stat .rate').each(function() {
	id = Number($(this).attr('data'));
	method = 'ra';
	
	if (had_action(method,id)) {
		$('#side .stat .rate').remove();
	}
});


// Page views application
$('.post-body').each(function() {
	id = Number($(this).attr('data'));
	method = 'vi';
	
	if (had_action(method,id)) return;
	
	$.post(home_url + '/ajax', {
		id:id,
		m:method
	},
	function(data){				
		set_action(method,id,data);
	});
});

